package com.primefaces.www.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.primefaces.www.dao.ImagesDAO;
import com.primefaces.www.dto.Gallery;
import com.primefaces.www.dto.Image;

@Controller
public class PhotoController {

	@Autowired
	private ImagesDAO imagesDAO;

	@RequestMapping(value = "/gallery/{galleryId}", method = RequestMethod.GET)
	public String galleryPageHandler(Locale locale, HttpServletRequest request,
			Model model, @PathVariable("galleryId") String galleryId) {
		List<Image> imageList = imagesDAO.getAllImages("1");
		List<String> images = new ArrayList<String>();
		if (imageList != null) {
			for (Image image : imageList) {
				images.add(image.getFilepath());
			}
		}
		model.addAttribute("images", imageList);
		return "photos/gallery";

	}

	@RequestMapping(value = "/json", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Gallery byProducesJson() {
		Gallery gallery = new Gallery();
		List<Image> imageList = imagesDAO.getAllImages("1");
		List<Image> images = new ArrayList<Image>();
		if (imageList != null) {
			for (Image image : imageList) {
				images.add(image);
			}
		}
		gallery.setImageList(images);
		return gallery;
	}

	public ImagesDAO getImagesDAO() {
		return imagesDAO;
	}

	public void setImagesDAO(ImagesDAO imagesDAO) {
		this.imagesDAO = imagesDAO;
	}

}
